package jdbc;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.JdbcUtil2;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class UtilsSelectBeanListHandler {
    public static void main(String[] args) throws SQLException {
        //创建dbUtils里面的QueryRunner对象
        QueryRunner queryRunner = new QueryRunner();
        //sql语句
        String sql = "select * from stu where age>?";
        //获取数据库连接
        Connection connection = JdbcUtil2.getConnection();
        //存参数值的数组
        Object[] params = {15};
        //执行查询，并以数组的形式返回查询结果（new BeanListHandler()返回查询到的所有记录，并转成对象）
        List<Student> students = queryRunner.query(connection,sql, new BeanListHandler<Student>(Student.class),params);
        System.out.println(students);
        //关闭数据库连接
        connection.close();
    }
}